package com.ease.common.util;

import java.util.StringTokenizer;

import com.ease.common.vo.MacAddress;

public abstract class MacAddressUtil extends Detect {

	public static final String SEPERATOR_COLON = ":";
	public static final String SEPERATOR_DOT = ".";
	public static final String SEPERATOR_HYPHEN = "-";

	public static MacAddress asMacAddress(String string) {
		if (null != string) {
			int len = string.length();
			StringTokenizer stringTokenizer = new StringTokenizer(string, SEPERATOR_COLON + SEPERATOR_DOT + SEPERATOR_HYPHEN);
			String[] addresses = null;
			if (14 == len) {
				addresses = new String[6];
				String element = stringTokenizer.nextElement().toString();
				addresses[0] = element.substring(0, 2);
				addresses[1] = element.substring(2, 4);
				element = stringTokenizer.nextElement().toString();
				addresses[2] = element.substring(0, 2);
				addresses[3] = element.substring(2, 4);
				element = stringTokenizer.nextElement().toString();
				addresses[4] = element.substring(0, 2);
				addresses[5] = element.substring(2, 4);

			} else if (17 == len) {
				addresses = new String[6];
				addresses[0] = stringTokenizer.nextElement().toString();
				addresses[1] = stringTokenizer.nextElement().toString();
				addresses[2] = stringTokenizer.nextElement().toString();
				addresses[3] = stringTokenizer.nextElement().toString();
				addresses[4] = stringTokenizer.nextElement().toString();
				addresses[5] = stringTokenizer.nextElement().toString();
			}
			if (null != addresses) {
				MacAddress macAddress = new MacAddress();
				macAddress.setAddresses(addresses);
				return macAddress;
			}
		}
		return null;
	}

	protected static String as3String(MacAddress macAddress, String seperator) {
		if (null != macAddress && null != macAddress.getAddresses()) {
			StringBuffer sb = new StringBuffer();
			String[] addresses = macAddress.getAddresses();
			sb.append(addresses[0]);
			sb.append(addresses[1]);
			sb.append(seperator);
			sb.append(addresses[2]);
			sb.append(addresses[3]);
			sb.append(seperator);
			sb.append(addresses[4]);
			sb.append(addresses[5]);
			return sb.toString();
		}
		return null;
	}

	protected static String as6String(MacAddress macAddress, String seperator) {
		if (null != macAddress && null != macAddress.getAddresses()) {
			StringBuffer sb = new StringBuffer();
			String[] addresses = macAddress.getAddresses();
			sb.append(addresses[0]);
			sb.append(seperator);
			sb.append(addresses[1]);
			sb.append(seperator);
			sb.append(addresses[2]);
			sb.append(seperator);
			sb.append(addresses[3]);
			sb.append(seperator);
			sb.append(addresses[4]);
			sb.append(seperator);
			sb.append(addresses[5]);
			return sb.toString();
		}
		return null;
	}

	public static String as3DotString(MacAddress macAddress) {
		return as3String(macAddress, SEPERATOR_DOT);
	}

	public static String as3HyphenString(MacAddress macAddress) {
		return as3String(macAddress, SEPERATOR_HYPHEN);
	}

	public static String as3ColonString(MacAddress macAddress) {
		return as3String(macAddress, SEPERATOR_COLON);
	}

	public static String as6HyphenString(MacAddress macAddress) {
		return as6String(macAddress, SEPERATOR_HYPHEN);
	}

	public static String as6ColonString(MacAddress macAddress) {
		return as3String(macAddress, SEPERATOR_COLON);
	}
}
